package com.eastshop.address.action;

import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.eastshop.address.service.AddressService;
import com.eastshop.address.vo.Address;
import com.eastshop.seller.service.RegionService;
import com.eastshop.seller.service.SellerService;
import com.eastshop.seller.vo.Region;
import com.eastshop.user.vo.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 收货地址的Action类
 * 
 * @author admin
 * 
 */
public class AddressAction extends ActionSupport implements
		ModelDriven<Address> {
	// 模型驱动管理的对象
	private Address address = new Address();

	public Address getModel() {
		return address;
	}

	// 注入AddressService
	private AddressService addressService;

	public void setAddressService(AddressService addressService) {
		this.addressService = addressService;
	}

	// 注入RegionServie
	private RegionService regionService;

	public void setRegionService(RegionService regionService) {
		this.regionService = regionService;
	}
	
	// 是否跳转页面
	private char isForward; //'0'：不转发 ，'1'：转发
	
	public void setIsForward(char isForward) {
		this.isForward = isForward;
	}

	// 查询我的收货地址
	public String findByUid() {
		// 从session中取出登录的用户
		User existUser = (User) ServletActionContext.getRequest().getSession()
				.getAttribute("existUser");
		// 根据用户id查找收货地址
		List<Address> addrList = addressService.findByUid(existUser.getUid());
		// 结果存入值栈
		ActionContext.getContext().getValueStack().set("addrList", addrList);
		// 查找商家服务的小区
		List<Region> regions = regionService.findRegion();
		// 结果存入值栈
		ActionContext.getContext().getValueStack().set("regions", regions);
		return "findByUid";
	}
	
	// 跳转到添加收货地址的页面
	public String insertAddr(){
		// 查找商家服务的小区
		List<Region> regions = regionService.findRegion();
		// 结果存入值栈
		ActionContext.getContext().getValueStack().set("regions", regions);
		return "insertAddr";
	}
	
	// 保存收货地址
	public String insertAddrSubmit() {
		// 从session中取出登录的用户
		User existUser = (User) ServletActionContext.getRequest().getSession()
				.getAttribute("existUser");
		// 设置收货地址所属的用户
		address.setUser(existUser);
		addressService.save(address);
		return isForward == '0' ? NONE : "insertAddrSubmit";
	}

	// 跳转到修改收货地址的页面
	public String editAddr() {
		// 根据收货地址id查找收货地址
		address = addressService.findByAid(address.getAid());
		// 结果存入值栈
		ActionContext.getContext().getValueStack().set("address", address);
		// 查找商家服务的小区
		List<Region> regions = regionService.findRegion();
		// 结果存入值栈
		ActionContext.getContext().getValueStack().set("regions", regions);
		return "editAddr";
	}

	// 提交修改的收货的地址
	public String editAddrSubmit() {
		// 从session中取出登录的用户
		User existUser = (User) ServletActionContext.getRequest().getSession()
				.getAttribute("existUser");
		// 设置关联的用户
		address.setUser(existUser);
		// 更新收货地址
		addressService.update(address);
		return NONE;
	}
	
	// 删除收货地址
	public String deleteAddr(){
		// 根据id查找收货地址
		address = addressService.findByAid(address.getAid());
		// 删除收货地址
		addressService.delete(address);
		return "deleteAddr";
	}
}
